#-*- coding:utf-8 -*-

from django.db import models
from django.contrib.auth.models import User


class Board(models.Model):
    name = models.CharField(max_length=100)
    desc = models.CharField(max_length=1024)
    banzhu = models.ForeignKey(User, null=True, blank=True)
    ts = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.name


class Article(models.Model):
    STATUS_NORMAL= 0
    STATUS_NOREPLY = 1
    STATUS_DELETED = 2
    STATUS_CHOICES = (
        (STATUS_NORMAL, "正常"),
        (STATUS_NOREPLY, "禁止回复"),
        (STATUS_DELETED, "删除"),
    )

    board = models.ForeignKey(Board)
    author = models.ForeignKey(User)
    title = models.CharField(max_length=100)
    content = models.TextField(max_length=4096)
    posttime = models.DateTimeField(auto_now=True)
    status = models.SmallIntegerField(choices=STATUS_CHOICES, default=STATUS_NORMAL)
    zans = models.PositiveIntegerField(default=0)
    saves = models.PositiveIntegerField(default=0)
    reads = models.PositiveIntegerField(default=0)
    comments = models.PositiveIntegerField(default=0)

    def __unicode__(self):
        return self.title


class Comment(models.Model):
    STATUS_NORMAL = 0
    STATUS_DELETED = 1
    STATUS_CHOICES = (
        (STATUS_NORMAL, "正常"),
        (STATUS_DELETED, "删除"),
    )

    article = models.ForeignKey(Article)
    author = models.ForeignKey(User, related_name="reader")
    content = models.TextField()
    posttime = models.DateTimeField(auto_now=True)
    status = models.SmallIntegerField(choices=STATUS_CHOICES, default=STATUS_NORMAL)

    def __unicode__(self):
        return self.content


class UserAct(models.Model):
    ACT_ZAN   = 1
    ACT_SAVE  = 2
    ACT_ALARM = 3
    act_status = (
        (ACT_ZAN, "赞"),
        (ACT_SAVE, "收藏"),
        (ACT_ZAN, "举报"),
    )

    user = models.ForeignKey(User)
    article = models.ForeignKey(Article)
    act = models.PositiveIntegerField()
    ts = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.act


class UserImg(models.Model):
    owner = models.ForeignKey(User)
    article = models.ForeignKey(Article, null=True, blank=True)
    image = models.ImageField(upload_to="forum")
    relative_path = models.CharField(max_length=128)
    size = models.PositiveIntegerField(default=0)
    posttime = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.relative_path


